package com.yphone.utils;

import android.text.TextUtils;

import com.google.gson.reflect.TypeToken;
import com.iiordanov.bVNC.Utils;
import com.yphone.cjencrypt.EncryptUtils;
import com.yphone.ui.login.bean.AccountInfo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class AccountUtils {
    private List<AccountInfo> accountInfos;
    private AccountInfo currentAccount;

    private static class LazyHolder {
        /* access modifiers changed from: private */
        public static final AccountUtils INSTANCE = new AccountUtils();

        private LazyHolder() {
        }
    }

    public List<AccountInfo> getAllAccount() {
//        if (this.accountInfos == null) {
//            this.accountInfos = (List) GsonUtils.fromJson(decString(SharepreferenceUtils.getSharedPreferencesToString(SharepreferenConstants.ACCOUNT_INFO_LIST, "")), new TypeToken<List<AccountInfo>>() {
//            }.getType());
//        }
        if (this.accountInfos == null) {
            this.accountInfos = new ArrayList();
        }
        return this.accountInfos;
    }

    public void putAccount(AccountInfo accountInfo) {
        boolean bFind = false;
        Iterator it = this.accountInfos.iterator();
        while (true) {
            if (!it.hasNext()) {
                break;
            }
            AccountInfo info = (AccountInfo) it.next();
            if (info.userName.equals(accountInfo.userName)) {
                info.passWord = accountInfo.passWord;
                bFind = true;
                break;
            }
        }
        if (!bFind) {
            this.accountInfos.add(0, accountInfo);
        }
        saveAccountList();
    }

    public void removeAccount(AccountInfo accountInfo) {
        Iterator<AccountInfo> it = this.accountInfos.iterator();
        while (true) {
            if (it.hasNext()) {
                if (((AccountInfo) it.next()).userName.equals(accountInfo.userName)) {
                    it.remove();
                    break;
                }
            } else {
                break;
            }
        }
        saveAccountList();
    }

    public AccountInfo getCurrentAccount() {
//        if (this.currentAccount == null) {
//            this.currentAccount = (AccountInfo) GsonUtils.fromJson(decString(SharepreferenceUtils.getSharedPreferencesToString(SharepreferenConstants.ACCOUNT_INFO_CURRENT, "")), AccountInfo.class);
//        }
        if (this.currentAccount == null) {
            this.currentAccount = new AccountInfo();
            this.currentAccount.userName = "";
            this.currentAccount.passWord = "";
        }
        return this.currentAccount;
    }

    public void saveCurrentAccount(AccountInfo accountInfo) {
        this.currentAccount = accountInfo;
        String accountStirng = "";
        if (this.currentAccount != null) {
            accountStirng = encString(GsonUtils.toJson(this.currentAccount));
        }
//        SharepreferenceUtils.setSharedPreferencesToString(SharepreferenConstants.ACCOUNT_INFO_CURRENT, accountStirng);
    }

    private void saveAccountList() {
        String accountStirng = "";
//        if (!Utils.isCollectionEmpty(this.accountInfos)) {
//            accountStirng = encString(GsonUtils.toJson(this.accountInfos));
//        }
//        SharepreferenceUtils.setSharedPreferencesToString(SharepreferenConstants.ACCOUNT_INFO_LIST, accountStirng);
    }

    private String encString(String inString) {
        String outString = "";
        if (TextUtils.isEmpty(inString)) {
            return outString;
        }
        try {
            return EncryptUtils.encode(inString);
        } catch (Exception e) {
            e.printStackTrace();
            return outString;
        }
    }

    private String decString(String inString) {
        String outString = "";
        if (TextUtils.isEmpty(inString)) {
            return outString;
        }
        try {
            return EncryptUtils.decode(inString);
        } catch (Exception e) {
            e.printStackTrace();
            return outString;
        }
    }

    private AccountUtils() {
        getAllAccount();
        getCurrentAccount();
    }

    public static AccountUtils getInstance() {
        return LazyHolder.INSTANCE;
    }

    /**
     * 时间格式化
     * @param time 小时
     * @return
     */
    public static String getDayAndHourTime(int time) {
        int day = time / 24;
        int hour = time % 24;
        if (day > 0 && hour >= 0) {
            return day + "天" + hour;
        }
        if (day != 0 || hour <= 0) {
            return "0";
        }
        return hour + "";
    }

}